#!/usr/bin/env Rscript

rm(list = ls())
options(stringsAsFactors=FALSE)

suppressMessages(library(dplyr))
suppressMessages(library(mediate))

setwd("")

cat("\nReading..\n")
## INDIV <- read.csv(
##     "formattedByIndividual_CaliAllYears_shorterFormatted.csv.gz"
## )
## "shorter" is subset to fewer variables
names(INDIV) <- gsub("cali3","cali",names(INDIV))

names(INDIV) <- c(names(INDIV)[-2],"extra")

formatYob <- function(x, c1, c2) {
    x <- as.integer(substr(x, c1, c2))
    x[x<1901|x>2000] <- NA
    return(x)
    }

cat("Formatting (1)..\n")
INDIV$yob07 <- formatYob(INDIV$dateofbirth_cali07, 1, 4)
INDIV$yob09 <- formatYob(INDIV$dateofbirth_cali09, 1, 4)
INDIV$yob10 <- formatYob(INDIV$dateofbirth_cali10, 1, 4)
INDIV$yob12 <- formatYob(INDIV$dateofbirth_cali12, 1, 4)
INDIV$yob14 <- formatYob(INDIV$dateofbirth_cali14, 1, 4)


INDIV$Age.log <- with(INDIV,scale(log(2008-yob07)))
INDIV$Age <- with(INDIV,((2008-yob07)))
INDIV$Changed.party <- with(INDIV,party_cali07!=party_cali09)
lm1 <- lm(mover07to09~Age.log, data=subset(INDIV, Age>=30 & Age<65 & party_cali07%in%c("dem","rep") & party_cali09%in%c("dem","rep")))
lm2 <- lm(Changed.party~Age.log+mover07to09, data=subset(INDIV, Age>=30 & Age<65 & party_cali07%in%c("dem","rep") & party_cali09%in%c("dem","rep")))
medmod <- mediate(lm1, lm2, treat = "Age.log",mediator="mover07to09",boot=F)

partySwitchSummary <- function(
    x=list(c("07","09"),c("09","10"),c("10","12"),c("12","14")),
    extrafilter="",
    mover=FALSE,
    voter=FALSE,
    lastvote=list(
        ## "05"="gg06_cali07",
        "07"="pg08_cali09",
        "09"="gg10_cali10",
        "10"="pg12_cali12",
        "12"="gg14_cali14"
    )) {
    return(
    lapply(x, function(x) {
        eval(
            parse(
                text=paste("INDIV %>%
    filter(
        !is.na(yob",x[1],")
        & !is.na(yob",x[2],")
        ",extrafilter,
                    ifelse(
                        voter,
                        paste("& ",lastvote[[x[1]]],"==1 "),
                        ""
                    ),
                    ifelse(
                        mover,
                        paste(
                            "& mover",x[1],"to",x[2],"==1",
                            sep=""),
                        ""
                    ),"
    ) %>%
        group_by(yob",x[1],") %>%
            summarise(
                majorparty=sum(
                    party_cali",x[1]," %in% c(\"dem\",\"rep\")
                    & party_cali",x[2]," %in% c(\"dem\",\"rep\")
                ),
                changeparty=sum(
                    party_cali",x[1],"!=party_cali",x[2],"
                    & party_cali",x[1]," %in% c(\"dem\",\"rep\")
                    & party_cali",x[2]," %in% c(\"dem\",\"rep\")
                ),
                endindependent=sum(
                    party_cali",x[1]," != \"\"
                    & party_cali",x[2]," != \"\"
                    & party_cali",x[2]," %in% c(\"ds\",\"npp\")
                ),
                toindependent=sum(
                    party_cali",x[1]," != \"\"
                    & party_cali",x[2]," != \"\"
                    & !(party_cali",x[1]," %in% c(\"ds\", \"npp\"))
                    & party_cali",x[2]," %in% c(\"ds\",\"npp\")
                ),
                startindependent=sum(
                    party_cali",x[1]," != \"\"
                    & party_cali",x[2]," != \"\"
                    & party_cali",x[1]," %in% c(\"ds\", \"npp\")
                ),
                fromindependent=sum(
                    party_cali",x[1]," != \"\"
                    & party_cali",x[2]," != \"\"
                    & party_cali",x[1]," %in% c(\"ds\", \"npp\")
                    & !(party_cali",x[2]," %in% c(\"ds\",\"npp\"))
                ),
                count=n(),
                movers=sum(mover",x[1],"to",x[2],"),
                majorpartydemstart=sum(
                    party_cali",x[1]," %in% c(\"dem\")
                    & party_cali",x[2]," %in% c(\"dem\",\"rep\")
                ),
                majorpartydemtorep=sum(
                    party_cali",x[1]," %in% c(\"dem\")
                    & party_cali",x[2]," %in% c(\"rep\")
                ),
                majorpartyrepstart=sum(
                    party_cali",x[1]," %in% c(\"rep\")
                    & party_cali",x[2]," %in% c(\"dem\",\"rep\")
                ),
                majorpartyreptodem=sum(
                    party_cali",x[1]," %in% c(\"rep\")
                    & party_cali",x[2]," %in% c(\"dem\")
                )
                )", sep = ""
                           )
                )
        )
        }
        )
    )
}

cat("Summarizing..\n")
i <- 0
pb <- txtProgressBar(style=3)
INDIV.partySummary <- partySwitchSummary()
i <- i+1/4; setTxtProgressBar(pb, i)
INDIV.partySummary.movers <- partySwitchSummary(mover=TRUE)
i <- i+1/4; setTxtProgressBar(pb, i)
INDIV.partySummary.voters <- partySwitchSummary(voter=TRUE)
i <- i+1/4; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.voters <- partySwitchSummary(mover=TRUE,voter=TRUE)
i <- i+1/4; setTxtProgressBar(pb, i)
close(pb)

cat("Writing..\n")
save(
    INDIV.partySummary,
    INDIV.partySummary.movers,
    INDIV.partySummary.voters,
    INDIV.partySummary.movers.voters,
    file="individual_partychanges_summary_withpartydirection.RData"
)

cat("\nDone (EOF).\n")
